package com.dstation.service;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.dstation.dao.ClassificationInfoMapper;
import com.dstation.domain.ClassificationInfo;
import com.dstation.domain.VideoInfo;
import com.dstation.service.interf.IClassificationService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.sql.Timestamp;
import java.util.List;
import java.util.Objects;

/**
 * @author:  Jeadon
 * @create:  2022/4/8 8:15
 * @tip:   分类Service层
 * @param:
 *
 **/
@Service
public class ClassificationServiceImpl  implements IClassificationService {

    @Resource
    private ClassificationInfoMapper classificationInfoMapper;


    @Override
    public boolean addClassification(  ClassificationInfo classificationInfo) {

        Timestamp time = new Timestamp(System.currentTimeMillis());
        return classificationInfoMapper.insertClassification(classificationInfo,time)==1;
    }

    @Override
    public boolean findIsClassificationNameRepeat(String classificationName) {
        return classificationInfoMapper.selectClassificationNameRepeat(classificationName)!=null;
    }

    @Override
    public boolean alterClassification(ClassificationInfo classificationInfo) {

        Timestamp time = new Timestamp(System.currentTimeMillis());
        return classificationInfoMapper.updateClassification(classificationInfo,time)==1;
    }

    @Override
    public boolean dropClassification(long classificationInfoId) {
        boolean isHaveChild=classificationInfoMapper.selectChildClassificationById(classificationInfoId).size()>0;
        if(isHaveChild){
            return false;
        }
        return classificationInfoMapper.deleteClassification(classificationInfoId)==1;
    }

    @Override
    public List<ClassificationInfo> findAllChildClassification() {
        return classificationInfoMapper.selectAllChildClassification();
    }

    @Override
    public List<ClassificationInfo> findClassificationByParentId(int parentId) {
        List<ClassificationInfo> classificationInfos = classificationInfoMapper.selectClassificationByParentId(parentId);
        if(classificationInfos.size() == 0) {
            return null;
        } else {
            return classificationInfos;
        }
    }

    @Override
    public IPage<ClassificationInfo> findClassification(String search, Page page) {
        if(Objects.isNull(search)){
            return classificationInfoMapper.selectClassification(search,page);
        }
        return classificationInfoMapper.selectClassification("%"+search+"%",page);
    }

    @Override
    public boolean findIsHavePreId(Long preClassificationId) {
        return classificationInfoMapper.selectClassificationByParentId(preClassificationId).size()>0;
    }

    @Override
    public Long findIdByName(String oldName) {
        return classificationInfoMapper.selectIdByName(oldName);
    }

    @Override
    public List<ClassificationInfo> findAllClassification() {
        return classificationInfoMapper.selectAllClassification();
    }
}
